package code.main.core.net
{
	import code.main.AppData;
	import code.structure.manager.others.StatisticsManager;
	import code.tools.traceLog.TraceLog;
	import code.untils.NetDataBackKeyConst;
	import code.untils.LogConst;
	
	import com.patience.net.Net;
	import com.patience.net.NetConst;
	import com.patience.net.events.NetErrorEvent;
	import com.patience.net.events.NetEvent;
	
	import flash.utils.getTimer;
	
	/**
	 *项目中后台交互层 (单例存在)
	 * @author Yuri
	 * 
	 */
	public class YNet extends Net
	{
		/**
		 *正常请求后台返回后回调 
		 */
		private var _netNormalCallBackFun:Function = null;
		/**
		 *请求后台出错回调 
		 */
		private var _netErrorCallBackFun:Function = null;
		
		private var _currentID:String;
		private var _allPackID:Array = []; //最后接收到的包ID
		
		public function YNet()
		{
			super();
		}
		
		/**
		 * 发送请求
		 * @param cmd 要发送的请求命令
		 * @param data 要发送的数据
		 * @param other 其他数据
		 * @param method post/get
		 * @param front 要追加到BODY前端的字符信息(仅socket可选,http不可用)
		 */		
		override public function send(cmd:String,data:Object,other:Object=null,method:String = NetConst.METHOD_FILE,front:String = ""):void
		{
			super.send(cmd,data,other,method);
			TraceLog.traceByTool("YNet::send::sendCommand",data);
		}
		
		public static function getIntance():YNet{
			if (!_instance) _instance = new YNet();
			_instance.requestURL = AppData.gateUrl
			return _instance as YNet;
		}
		/**
		 * 设置回调函数
		 * @param	completeFun
		 * @param	errorFun
		 */
		public function setCallBackFuns(completeFun:Function, errorFun:Function):void {
			_netNormalCallBackFun = completeFun;
			_netErrorCallBackFun = errorFun;
		}
		
		override protected function onIOErrorHandler(event:NetErrorEvent):void
		{
			super.onIOErrorHandler(event);
			TraceLog.traceByTool("YNet::onIOErrorHandler::dataError",event.data);
		}
		/**
		 *信息返回 
		 * @param event
		 * 
		 */
		override protected function onDataCompleteHandler(event:NetEvent):void{
			if(int(event.data[1].command) != 1)
				TraceLog.traceByTool("YNet::onDataCompleteHandler::dataComplete",event.data);
			else
				TraceLog.traceByTool("YNet::onDataCompleteHandler::dataComplete",["command = 1 类型数据已返回"]);
			analysisData(event.data as Array);
		}
		/**
		 *分析返回数据 
		 * @param data
		 * 
		 */
		private function analysisData(data:Array):void{
			
			//TraceLog.showTrace([getTimer(),data],"返回时间");
			
			if(!data || data.length==0) return;
			if (int(data[0]) == 1) {  //数据准确
				
				_currentID = data[1].requestid;
				if(_allPackID.indexOf(_currentID) > -1) {
					return;
				}
				_allPackID.push(_currentID);
				if(_allPackID.length > 50) _allPackID.splice(0,30);
				
				//检测是否需要解析回调数据
				var isDispatchData:Boolean = false;
				isDispatchData = int(data[1].command) == 1;
				if (_netNormalCallBackFun != null) _netNormalCallBackFun(data[1]);
				if(data.length>2 && isDispatchData){ //有返回参数
					var source:Object = data[2] as Object;
					var nE:NetEvent;
					if(int(data[1].requestid)==NetManager.FIRST_BASEINIT_REQUEST_ID && NetManager.IS_REQUEST_FIRST_BASE_INIT){ //如果是首次加载baseinit
						nE = new NetEvent(NetDataBackKeyConst.BASE_INIT_FIRST,source);//派发数据
						dispatchEvent(nE);
						return ;
					}
					for(var key:String in source){
						nE = new NetEvent(key,source[key]);//派发数据
						dispatchEvent(nE);
					}
				}else{
					trace("命令返回===>>>", String(data));
				}
			}else {
				var errType:int = int(data[0]);
				if((errType >= -999 && errType <= -500) || errType == -1001) {
					StatisticsManager.writeLog(LogConst.ERROR_NO_EFFECT_TYPE,errType);
					return;
				}
				
				if (_netErrorCallBackFun != null)_netErrorCallBackFun(errType);
				
				var nErrorE:NetErrorEvent = new NetErrorEvent(NetErrorEvent.EVENT_NET_ERROR,data[0]);
				dispatchEvent(nErrorE);
			}
		}
		/**
		 *派发第二层数据 
		 * @param data
		 * 
		 */
		private function dispatchSecondData(data:Object):void{
			for(var key:String in data){
				var nE:NetEvent = new NetEvent(key, data[key]);//派发数据
				dispatchEvent(nE);
			}
		}
		
	}
}